package cn.itsource.pet.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.GetRedisLoginVoUtil;
import cn.itsource.basic.util.PageList;
import cn.itsource.pet.mapper.SearchMasterMessageMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.pojo.domain.SearchMasterMessage;
import cn.itsource.pet.pojo.domain.SearchMasterMsg;
import cn.itsource.pet.query.SearchMasterMessageQuery;
import cn.itsource.pet.service.ISearchMasterMessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
//Propagation.SUPPORTS:支持别人事务
@Transactional
public class SearchMasterMessageServiceImpl extends BaseServiceImpl<SearchMasterMessage, SearchMasterMessageQuery> implements ISearchMasterMessageService {

    @Autowired
    SearchMasterMessageMapper messageMapper;

    @Autowired
    SearchMasterMsgMapper msgMapper;

    @Override
    public AjaxResult shenghe(SearchMasterMessage message) {
        //1.如果是驳回，只修改状态就ok了
        Integer state = message.getState();
        if (state == 2){
            messageMapper.updateByPrimaryKeySelective(message);
            return AjaxResult.createSuccess();
        }

        //2.如果是审核通过 修改状态
        messageMapper.updateByPrimaryKeySelective(message);

        SearchMasterMessage dbSearchMasterMessage = messageMapper.selectByPrimaryKey(message.getId());
        dbSearchMasterMessage.setState(message.getState());

        //2.1数据入库到接单表
        SearchMasterMsg searchMasterMsg = BeanUtil.copyProperties(dbSearchMasterMessage, SearchMasterMsg.class);
        searchMasterMsg.setState(0);//写死：未接单
        msgMapper.insertSelective(searchMasterMsg);

        return AjaxResult.createSuccess();
    }

    /**
     * 分页
     * @param query 高级查询条件
     */
    @Override
    public PageList<SearchMasterMessage> findPage(SearchMasterMessageQuery query) {
        //1.分页插件
        PageHelper.startPage(query.getPage(),query.getSize());
        //2.查询
        List<SearchMasterMessage> searchMasterMessagePageList = messageMapper.pageList(query);
        //3.封装pageInfo对象
        PageInfo<SearchMasterMessage> pageInfo = new PageInfo<>(searchMasterMessagePageList);

        //4.封装自定义的分页对象
        PageList<SearchMasterMessage> pageList = new PageList<>(pageInfo.getTotal(), pageInfo.getList());

        return pageList;
    }

    //寻主发布入库
    @Override
    public AjaxResult publish(SearchMasterMessage message) {

        //设置userId
        Long userId = GetRedisLoginVoUtil.getUserId();
        message.setUserId(userId);
        //入库
        messageMapper.insertSelective(message);

        return AjaxResult.createSuccess();
    }


}
